package com.corpgovernment.job.dao.mapper;

import com.corpgovernment.job.dao.entity.db.JobTypeDo;
import com.corpgovernment.job.enums.DeletedEnum;
import com.corpgovernment.organization.common.mybatis.TkMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ldma
 * @date 2024/1/2
 */
public interface JobTypeMapper extends TkMapper<JobTypeDo> {

    default void create(JobTypeDo jobTypeDo) {
        insertSelective(jobTypeDo);
    }

    default void removeById(Long id) {
        if (id == null) {
            return;
        }
        JobTypeDo jobTypeDo = new JobTypeDo();
        jobTypeDo.setId(id);
        jobTypeDo.setDeleted(DeletedEnum.TRUE.getCode());
        updateByPrimaryKeySelective(jobTypeDo);
    }

    default void update(JobTypeDo jobTypeDo) {
        updateByPrimaryKeySelective(jobTypeDo);
    }

    default List<JobTypeDo> listAll() {
        Example example = new Example(JobTypeDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        example.orderBy("id").desc();
        return selectByExample(example);
    }

    default JobTypeDo getByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        Example example = new Example(JobTypeDo.class);
        example.createCriteria().andEqualTo("name", name).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        List<JobTypeDo> jobTypeDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobTypeDoList) ? null : jobTypeDoList.get(0);
    }

    default JobTypeDo getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        Example example = new Example(JobTypeDo.class);
        example.createCriteria().andEqualTo("code", code).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        List<JobTypeDo> jobTypeDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobTypeDoList) ? null : jobTypeDoList.get(0);
    }

    default JobTypeDo getById(Long id) {
        if (id == null) {
            return null;
        }
        Example example = new Example(JobTypeDo.class);
        example.createCriteria().andEqualTo("id", id).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        List<JobTypeDo> jobTypeDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobTypeDoList) ? null : jobTypeDoList.get(0);
    }

    default List<JobTypeDo> listByIdList(List<Long> jobTypeIdList) {
        if (CollectionUtils.isEmpty(jobTypeIdList)) {
            return new ArrayList<>(0);
        }
        Example example = new Example(JobTypeDo.class);
        example.createCriteria().andIn("id", jobTypeIdList).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

}
